package com.czy.owner.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import com.czy.owner.utils.glide.config.GlideCacheConfig;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by wangyang on 2017/4/28 0028.
 */

public class AssetsFileUtils {
    /**
     * 读取Assets文件夹中的图片资源
     *
     * @param context
     * @param fileName
     * @return
     */
    public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
        //获取应用的包名
//        String packageName = context.getPackageName();
        //定义存放这些图片的内存路径
        String path = Environment.getExternalStorageDirectory() + GlideCacheConfig.GLIDE_CARCH_DIR;
        //如果这个路径不存在则新建
        File file = new File(path);
        Bitmap image = null;
        boolean isExist = file.exists();
        if (!isExist) {
            file.mkdirs();
        }
        //获取assets下的资源
        AssetManager am = context.getAssets();
        try {
            //图片放在img文件夹下
            InputStream is = am.open(fileName);
            image = BitmapFactory.decodeStream(is);
            FileOutputStream out = new FileOutputStream(path + "/" + fileName);
            //这个方法非常赞
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }
}
